package com.xk857.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xk857.config.AuthUtil;
import com.xk857.entities.SysUser;
import com.xk857.entities.Tourism;
import com.xk857.feign.IFeignSystemController;
import com.xk857.school.mapper.TourismMapper;
import com.xk857.school.req.TourismREQ;
import com.xk857.school.req.TourismUserREQ;
import com.xk857.school.service.ITourismService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xk857.util.base.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 组团旅游 服务实现类
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-02
 */
@Primary
@Service
public class TourismServiceImpl extends ServiceImpl<TourismMapper, Tourism> implements ITourismService {

    @Autowired
    private IFeignSystemController feignSystemController;

    @Override
    public Result searchListPub(TourismREQ req) {
        QueryWrapper<Tourism> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(req.getTitle())) {
            wrapper.like("title", req.getTitle());
        }
        if (StringUtils.isNotEmpty(req.getSchoolName())) {
            wrapper.like("school_name", req.getSchoolName());
        }
        if (StringUtils.isNotEmpty(req.getUserId())) {
            wrapper.eq("user_id", req.getUserId());
        }
        wrapper.gt("end_time", new Date());
        wrapper.orderByDesc("create_time");
        return Result.ok(baseMapper.selectPage(req.getPage(), wrapper));
    }

    @Override
    public Result addByUser(TourismUserREQ req) {

        if (StringUtils.isEmpty(req.getTitle())) {
            return Result.error("标题不可为空");
        }

        // 用户信息
        SysUser userInfo = AuthUtil.getUserInfo();
        SysUser user = feignSystemController.findUserByUserId(userInfo.getId());

        Tourism tourism = new Tourism();
        tourism.setTitle(req.getTitle());
        tourism.setComtent(req.getComtent());
        tourism.setAddress(req.getAddress());
        tourism.setUserId(user.getId());
        if (req.getPhone() == null) {
            tourism.setPhone(user.getMobile());
        } else {
            tourism.setPhone(req.getPhone());
        }
        tourism.setSchoolName(req.getSchoolName());
        tourism.setPeopleNum(req.getPeopleNum());
        tourism.setNickName(user.getNickName());
        tourism.setUserImg(user.getImageUrl());
        tourism.setMdContent(req.getMdContent());
        tourism.setCreateTime(new Date());
        tourism.setStatus("1");
        tourism.setEndTime(req.getEndTime());
        baseMapper.insert(tourism);
        return Result.ok();
    }

    @Override
    public Result add(Tourism tourism) {
        baseMapper.insert(tourism);
        return Result.ok(tourism.getId());
    }

    @Override
    public Result update(Tourism tourism) {
        baseMapper.updateById(tourism);
        return Result.ok(tourism.getId());
    }

    @Override
    public Result selectById(String id) {
        return Result.ok(baseMapper.selectById(id));
    }

    @Override
    public Result deleteById(String id) {
        baseMapper.deleteById(id);
        return  Result.ok();
    }

    @Override
    public Result viewAdd(String id) {
        Tourism tourism = baseMapper.selectById(id);
        tourism.setViewNum(tourism.getViewNum() + 1);
        baseMapper.updateById(tourism);
        return Result.ok();
    }
}
